package com.sy.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sy.EmployeeService;
import com.sy.constant.MessageConstant;
import com.sy.constant.StatusConstant;
import com.sy.context.BaseContext;
import com.sy.dto.EmployeeDTO;
import com.sy.dto.EmployeeLoginDTO;
import com.sy.dto.EmployeePageQueryDTO;
import com.sy.mapper.EmployeeMapper;
import com.sy.pojo.Employee;
import com.sy.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
@Slf4j
public class EmployeeServiceImpl
        extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    // 登录
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 1.根据用户名查询用户
        queryWrapper.eq(Employee::getUsername, employeeLoginDTO.getUsername());
        Employee employee = this.getOne(queryWrapper);
        if (employee == null){
            throw new RuntimeException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //2.前台获取到的密码进行加密
        String password = DigestUtils.md5DigestAsHex(employeeLoginDTO.getPassword().getBytes());
        //密码比对
        if (!password.equals(employee.getPassword())){
            throw new RuntimeException(MessageConstant.PASSWORD_ERROR);
        }
        //3.判断账号是否被锁定
        if (employee.getStatus() == StatusConstant.DISABLE){
            throw new RuntimeException(MessageConstant.ACCOUNT_LOCKED);
        }
        //4.判断账号是否被注销
        if (employee.getIsDelete() == StatusConstant.DISABLE){
            throw new RuntimeException("您的账号已被注销");
        }
        return employee;
    }

    // 员工分页查询
    @Override
    public PageResult findAll(EmployeePageQueryDTO employeePageQueryDTO) {
        //根据用户名姓名模糊查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //如果empDTO.getName()不为空,那么加入模糊查询条件
        queryWrapper.like(StringUtils.isNotEmpty(employeePageQueryDTO.getName()),
                Employee::getName, employeePageQueryDTO.getName())
                .select(Employee.class, info->!info.getColumn().equals("password"));
        //配置分页信息，进行分页查询
        Page<Employee> page = this.page(new Page<>(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize()), queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    // 新增员工
    @Override
    public void saveEmp(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        this.save(employee);
    }
}
